package com.qttaudio.example.dialog;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.qttaudio.example.RoomActivity;
import com.qttaudio.example.data.MicSeatBean;
import com.qttaudio.ios.demo.R;


public class MicPopupWindow extends PopupWindow {
    private MicSeatBean micSeatBean;
    private int position;
    private PopOnclick popOnclick;
    private TextView tv_mic_pop_one;
    private TextView tv_mic_pop_line;
    private TextView tv_mic_pop_two;


    public MicPopupWindow(Context context) {
        this(context,0, null);
    }

    public MicPopupWindow(Context context,int position, MicSeatBean micSeatBean) {
        super(context);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        setOutsideTouchable(true);
        setFocusable(true);
        setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        View contentView = LayoutInflater.from(context).inflate(R.layout.layout_room_mic_item_pop,
                null, false);
        initView(contentView,position, micSeatBean);
        this.position = position;
        setContentView(contentView);
    }

    private void initView(View contentView, final int position, final MicSeatBean micSeatBean) {
        tv_mic_pop_one = contentView.findViewById(R.id.tv_mic_pop_one);
        tv_mic_pop_line = contentView.findViewById(R.id.tv_mic_pop_line);
        tv_mic_pop_two = contentView.findViewById(R.id.tv_mic_pop_two);

        if (micSeatBean == null) {
            tv_mic_pop_line.setVisibility(View.GONE);
            tv_mic_pop_two.setVisibility(View.GONE);
            tv_mic_pop_one.setText(R.string.room_view_mic_pop_take_mic);
        } else if (micSeatBean.getUid() == RoomActivity.mUid) {
            tv_mic_pop_line.setVisibility(View.GONE);
            tv_mic_pop_two.setVisibility(View.GONE);
            tv_mic_pop_one.setText(R.string.room_view_mic_pop_wheat_mic);
        } else if (micSeatBean.isOnly_me_mute() ) {
            tv_mic_pop_line.setVisibility(View.GONE);
            tv_mic_pop_two.setVisibility(View.GONE);
            tv_mic_pop_one.setText(R.string.room_view_mic_pop_cancel_mute);
        } else if (micSeatBean.isPrivate_chat()) {
            tv_mic_pop_line.setVisibility(View.GONE);
            tv_mic_pop_two.setVisibility(View.GONE);
            tv_mic_pop_one.setText(R.string.room_view_mic_pop_cancel_private);
        } else if (micSeatBean.isMute()) {
//            tv_mic_pop_line.setVisibility(View.GONE);
//            tv_mic_pop_two.setVisibility(View.GONE);
//            tv_mic_pop_one.setText(R.string.room_view_mic_pop_private_chat);
            contentView.setVisibility(View.GONE);
        } else {
            tv_mic_pop_one.setText(R.string.room_view_mic_pop_mute);
            tv_mic_pop_line.setVisibility(View.GONE);
            tv_mic_pop_two.setVisibility(View.GONE);
//            tv_mic_pop_two.setText(R.string.room_view_mic_pop_private_chat);
        }
        tv_mic_pop_one.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MicPopupWindow.this.dismiss();
                if (popOnclick != null) {
                    if (micSeatBean == null) {
                        popOnclick.onClickTakeMic(position);
                    } else if (micSeatBean.getUid() == RoomActivity.mUid) {
                        popOnclick.onClickWheatMic(position,micSeatBean);
                    } else if (micSeatBean.isOnly_me_mute()) {
                        popOnclick.onClickCancelMute(position,micSeatBean);
                    } else if (micSeatBean.isPrivate_chat()) {
                        popOnclick.onClickCancelPrivate(position,micSeatBean);
                    } else if (micSeatBean.isMute()) {
                        popOnclick.onClickPrivateChat(position,micSeatBean);
                    } else {
                        popOnclick.onClickMute(position,micSeatBean);
                    }
                }
            }
        });
        tv_mic_pop_two.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MicPopupWindow.this.dismiss();
                if (popOnclick != null) {
                    popOnclick.onClickPrivateChat(position,micSeatBean);
                }
            }
        });

    }

    public void setPopOnclick(PopOnclick popOnclick) {
        this.popOnclick = popOnclick;
    }

    public int getPosition() {
        return position;
    }


    public interface PopOnclick {
        /**
         * 上麦
         */
        void onClickTakeMic(int position);

        /**
         * 下麦
         * @param micSeatBean
         */
        void onClickWheatMic(int position,MicSeatBean micSeatBean);
        /**
         * 取消静音
         * @param micSeatBean
         */
        void onClickCancelMute(int position,MicSeatBean micSeatBean);
        /**
         * 静音
         * @param micSeatBean
         */
        void onClickMute(int position,MicSeatBean micSeatBean);
        /**
         * 私聊
         * @param micSeatBean
         */
        void onClickPrivateChat(int position,MicSeatBean micSeatBean);
        /**
         * 取消私聊
         * @param micSeatBean
         */
        void onClickCancelPrivate(int position,MicSeatBean micSeatBean);


    }

    /**
     *  在android7.0上，如果不主动约束PopuWindow的大小，比如，设置布局大小为 MATCH_PARENT,那么PopuWindow会变得尽可能大，以至于 view下方无空间完全显示PopuWindow，而且view又无法向上滚动，此时PopuWindow会主动上移位置，直到可以显示完全。
     *　解决办法：主动约束PopuWindow的内容大小，重写showAsDropDown方法：
     * @param anchor
     */
    @Override
    public void showAsDropDown(View anchor) {
        if(Build.VERSION.SDK_INT >= 24){
            Rect visibleFrame = new Rect();
            anchor.getGlobalVisibleRect(visibleFrame);
            int height = anchor.getResources().getDisplayMetrics().heightPixels - visibleFrame.bottom;
            setHeight(height);
        }
        super.showAsDropDown(anchor);
    }


}